package com.archie.mongodb;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Date;

import org.bson.Document;
import org.bson.conversions.Bson;

import com.ls.fw.mongo.helper.MongoDbHelper;
import com.mongodb.client.model.Filters;

public class Main {

	public static void main(String[] args) {
		
	}
	
	 
	
	
	/**
	 * Document转实体类
	 * @param <T>
	 * 
	 * @param doc
	 * @param cls
	 * @return
	 */
	public static <T> T documentToEntity(Document doc, Class<T> cls) {
		T t = null;
		try {
			t = cls.newInstance();
			Field[] fields = cls.getDeclaredFields();
			for (Field field : fields) {
				String name = field.getName();
				String firstLetter = name.substring(0, 1).toUpperCase();
				String setMethodName = "set" + firstLetter + name.substring(1);
				Method setMethod = cls.getDeclaredMethod(setMethodName, new Class[] { field.getType() });// 获取set方法
				String typeStr=field.getGenericType().toString();
//				Object value=null;
//				if (typeStr.equals("class java.lang.Integer")) {
//					value=(Integer)doc.get(name);
//				}
				setMethod.invoke(t, new Object[] { doc.get(name) });
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return t;
	}
}
